<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <role-search @search="reload" />
      <!-- 数据表格 -->

      <ele-pro-table ref="table"
                     :columns="columns"
                     :datasource="datasource"
                     :selection.sync="selection"
                     tool-class="ele-toolbar-form">
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>

        </template>

        <template v-slot:img="{ row }">
          <div><img class="categoryImages"
                 :src="row.img"
                 alt="" /></div>
        </template>
        <template v-slot:content="{ row }">
          <div v-html="row.content"></div>
        </template>

        <!-- status == 0 ? '全部' : status == 1 ? '待接单' : status == 2 ? '接单中' : status == 3 ? '完成' : status == 4 ? '已结算' : '拒绝订单'; -->
        <template v-slot:status="{ row }">
          <el-tag v-if="row.order_status == 0"
                  type="primary"
                  :underline="false">
            全部
          </el-tag>
          <el-tag v-if="row.order_status == 1"
                  type="warning"
                  :underline="false">
            待接单
          </el-tag>
          <el-tag v-if="row.order_status == 2"
                  type="primary"
                  :underline="false">
            接单中
          </el-tag>
          <el-tag v-if="row.order_status == 3"
                  type="danger"
                  :underline="false">
            完成
          </el-tag>

          <el-tag v-if="row.order_status ==4"
                  type="primary"
                  :underline="false">
            已结算
          </el-tag>
          <el-tag v-if="row.order_status ==5"
                  type="danger"
                  :underline="false">
            拒绝订单
          </el-tag>
        </template>
        <!-- <template v-slot:status="{ row }">
          {{ orderStatusFn(row.order_status) }}
        </template> -->
        <!-- 状态列 -->
        <template v-slot:sex="{ row }">
          <el-link v-if="row.sex == 0"
                   type="success"
                   :underline="false">
            性别不限
          </el-link>
          <el-link v-if="row.sex == 1"
                   type="primary"
                   :underline="false">
            男
          </el-link>
          <el-link v-if="row.sex == 2"
                   type="danger"
                   :underline="false">
            女
          </el-link>

        </template>
        <!-- <template v-slot:sex="{ row }">
          {{ row.sex==0?'性别不限':row.sex==1?'男':'女' }}
        </template> -->
        <template v-slot:level="{ row }">
          <div>
            {{ row.level==0?'普通用户':'vip用户' }}
          </div>
        </template>
        <template v-slot:ru_nickname="{ row }">
          <div v-if="row.ru_nickname">
            <div class="realNameStyle">
              <img class="avaterName"
                   :src="row.ru_avatar"
                   alt="" />
              <span>{{ row.ru_nickname }}</span>
            </div>
          </div>
          <div v-else>-----</div>
        </template>
        <template v-slot:ju_nickname="{ row }">
          <div v-if="row.ju_nickname">
            <div class="realNameStyle">
              <img class="avaterName"
                   :src="row.ju_avatar"
                   alt="" />
              <span>{{ row.ju_nickname }}</span>
            </div>
          </div>
          <div v-else>-----</div>
        </template>
        <template v-slot:sort="{ row }">
          <i class="sort-handle el-icon-rank ele-text-secondary"
             style="cursor: move"></i>
          <div v-if="false">{{ row }}</div>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link type="primary"
                   :underline="false"
                   @click="openEdit(row)">
            详情
          </el-link>

          <el-link v-if="row.order_status==1"
                   type="danger"
                   :underline="false"
                   @click="refuseOrder(row)">
            拒绝接单
          </el-link>
          <el-popconfirm class="ele-action"
                         title="确定要删除该条信息？"
                         @confirm="confirmReceipt(row)">
            <template v-slot:reference>
              <el-link v-if="row.order_status==3"
                       type="success"
                       :underline="false">
                确定收货
              </el-link>
            </template>
          </el-popconfirm>

          <el-link v-if="row.order_status==3||row.order_status==4"
                   type="primary"
                   :underline="false"
                   @click="snapshot(row)">
            订单完成描述
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <Camilist :data="current1"
              :drawerEditBoolen.sync="drawerEditBoolen1"
              @done="reload" />
    <!-- 编辑弹窗 -->
    <role-edit :data="current"
               :drawerEditBoolen.sync="drawerEditBoolen"
               @done="reload" />

    <el-dialog title="拒绝原因"
               :visible.sync="centerDialogVisible"
               width="30%"
               center>
      <el-input type="textarea"
                :rows="2"
                resize="none"
                placeholder="请输入拒绝原因"
                v-model="textarea">
      </el-input>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="refuseOrderSureFn()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="订单快照"
               :visible.sync="snapshotDialogVisible"
               width="50%"
               center>
      <div style="text-align: center;">
        <el-input type="textarea"
                  :disabled="true"
                  resize="none"
                  v-model="snapshotDataRemark">
        </el-input>
        <img @click="handlePictureCardPreview(snapshotData?.complete_img)"
             style="max-width: 100%; max-height: 100%; object-fit: contain;margin-top: 20px;"
             :src="snapshotData?.complete_img"
             alt="">
      </div>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="snapshotDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="snapshotDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <el-image-viewer v-if="viewerVisible"
                     style="z-index: 9999"
                     :on-close="closeViewer"
                     :url-list="imgViewArr" />
    <!-- 权限分配弹窗 -->
  </div>

</template>
	<script>
import ElImageViewer from 'element-ui/packages/image/src/image-viewer';

import RoleSearch from './components/role-search';
// import Sortable from 'sortablejs';
import RoleEdit from './components/role-edit';
import { carouselList } from './constant/constant';
import Camilist from './components/Camilist';
import { serviceOrder, refuseServiceOrder, settlementService } from '@/api/serviceManagement/index'

// import {
//   sitecCategoriesDelete,
//   siteCategoriesList,
//   siteCategoriesModify,
//   siteCategoriesDetail,
//   siteBatchCategoriesDelete
// } from '@/api/serviceManagement/categoriesList';
export default {
  name: 'Recharge',
  components: {
    RoleSearch,
    RoleEdit,
    Camilist,
    ElImageViewer

  },
  data () {
    return {
      form: {
        number: 0
      },
      carouselList,
      centerDialogVisible: false,
      snapshotDialogVisible: false,
      // 表格列配置
      columns: [

        // {
        //   prop: 'id',
        //   label: '排序',
        //   sortable: 'custom',
        //   showOverflowTooltip: true,
        //   minWidth: 30,
        //   align: 'center',
        //   slot: 'sort'
        // },


        {
          prop: 'son_tab_name',
          label: '二级分类名字',
          sortable: 'custom',
          width: 105,
          align: 'center',
          showOverflowTooltip: true,


        },
        {
          prop: 'title',
          label: '服务标题',
          sortable: 'custom',
          width: 105,
          align: 'center',
          showOverflowTooltip: true,


        },
        {
          prop: 'address',
          label: '地点',
          sortable: 'custom',
          align: 'center',
          showOverflowTooltip: true,
          width: 205,
        },
        {
          prop: 'ru_nickname',
          label: '服务创建者',
          sortable: 'custom',
          align: 'center',
          showOverflowTooltip: true,
          width: 125,
          slot: 'ru_nickname'
        },
        {
          prop: 'ju_nickname',
          label: '下单用户',
          sortable: 'custom',
          align: 'center',
          showOverflowTooltip: true,
          width: 125,
          slot: 'ju_nickname'
        },


        {
          prop: 'price',
          label: '价格',
          align: 'center',
          sortable: 'custom',

          showOverflowTooltip: true,

        },

        {
          prop: 'status',
          label: '当前状态',
          align: 'center',
          sortable: 'custom',

          showOverflowTooltip: true,
          slot: 'status'

        },
        {
          prop: 'label',
          label: '标签需求',
          align: 'center',
          sortable: 'custom',

          showOverflowTooltip: true,

        },
        {
          prop: 'sex',
          label: '标签需求',
          align: 'center',
          sortable: 'custom',

          showOverflowTooltip: true,
          slot: 'sex'
        },

        {
          prop: 'create_time',
          label: '创建时间',
          align: 'center',
          sortable: 'custom',
          showOverflowTooltip: true,
          formatter: (_row, _column, cellValue) => {
            return this.$util.toDateString(cellValue, 'yyyy-MM-dd HH:mm');
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 205,
          align: 'center',
          resizable: false,
          slot: 'action',
          hideInSetting: true,
          showOverflowTooltip: true
        }
      ],
      textarea: '',
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      current1: null,
      drawerEditBoolen: false,
      // 是否显示编辑弹窗
      drawerEditBoolen1: false,
      // 是否显示导入弹窗
      showAuth: false,
      showChoose: false,
      dialogVisible: false,
      levelId: null,
      refuseOrderId: null,
      snapshotData: null,
      snapshotDataRemark: null,
      viewerVisible: false,
      imgViewArr: null
    };
  },
  mounted () {

  },
  activated () {
    this.reload();
  },
  computed: {
    orderStatusFn: function () {
      return function (status) {

        let orderStatus = '';
        // 1 待接单 2 接单中 3 完成 4 已结算 4拒绝订单
        orderStatus =
          status == 0 ? '全部' : status == 1 ? '待接单' : status == 2 ? '接单中' : status == 3 ? '完成' : status == 4 ? '已结算' : '拒绝订单';

        return orderStatus;
      };
    },

  },

  methods: {
    confirmReceipt (row) {
      settlementService({
        id: row.order_id,


      })
        .then((msg) => {
          console.log(msg)

          this.$message.success(msg.message);
          this.reload();
        })
        .catch((e) => {


          this.$message.error(e.message);
        });
    },
    handlePictureCardPreview (imgStr) {

      console.log(imgStr);
      if (imgStr.length) {
        if (imgStr.includes(',')) {
          console.log('字符串中包含逗号');
          // 包含的进行slice(',') 分割

          this.imgViewArr = imgStr.split(',').map((item) => item.trim());
        } else {
          console.log('字符串中不包含逗号');
          this.imgViewArr = imgStr.split();
        }

        this.viewerVisible = true;
      } else {
        this.$message({
          message: '没有上传图片无法进行预览',
          type: 'warning'
        });
      }
    },
    // 关闭查看器
    closeViewer () {
      this.viewerVisible = false;
    },
    snapshot (row) {
      this.snapshotData = row
      this.snapshotDataRemark = row.remark
      this.snapshotDialogVisible = !this.snapshotDialogVisible
    },
    refuseOrderSureFn () {

      if (this.textarea) {
        const loading = this.$loading({ lock: true });
        refuseServiceOrder({
          id: this.refuseOrderId,
          remark: this.textarea,

        })
          .then((msg) => {
            console.log(msg)
            loading.close();
            this.$message.success(msg.message);
            this.reload();
          })
          .catch((e) => {
            loading.close();

            this.$message.error(e.message);
          });
      } else {
        this.$message.error('请填写拒绝原因');
      }

    },

    // rowDrop () {
    //   const tbody = document.querySelector('.el-table__body-wrapper tbody');
    //   const _this = this;
    //   Sortable.create(tbody, {
    //     forceFallback: true,
    //     dragClass: 'drag',
    //     delay: 100,
    //     animation: 1000,
    //     onEnd ({ newIndex, oldIndex }) {
    //       console.log(newIndex);
    //       console.log(oldIndex);
    //       //更换表格数据 后续拖动坐标才会正确
    //       let data = _this.$refs.table.getData();

    //       let nid = data[newIndex].id;
    //       let oid = data[oldIndex].id;

    //       const currRow = data.splice(oldIndex, 1)[0];
    //       data.splice(newIndex, 0, currRow);

    //       console.log(data);

    //       //   sortChange(data)
    //       //     .then((msg) => {

    //       //     })
    //       //     .catch((e) => {
    //       //       _this.$refs.table.reload();
    //       //     });

    //       //去数据库更换排序

    //       console.log(_this.$refs.table.getData());
    //     }
    //   });
    // },
    async openCami (row) {
      console.log(row);
      if (row?.id) {
        // const res = await siteCategoriesDetail(row.id);

        this.current1 = row;
        // 用户余额管理
        this.drawerEditBoolen1 = !this.drawerEditBoolen1;
      } else {
        this.drawerEditBoolen1 = !this.drawerEditBoolen1;
        this.current1 = null;
      }
    },
    /* 表格数据源 */
    datasource ({ page, limit, where }) {
      return serviceOrder({
        page,
        limit,
        ...where,

      });
    },
    /* 刷新表格 */
    reload (where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 显示编辑 */
    async openEdit (row) {
      console.log(row);
      if (row?.id) {
        // const res = await siteCategoriesDetail(row.id);

        this.current = row;
        // 用户余额管理
        this.drawerEditBoolen = !this.drawerEditBoolen;
      } else {
        this.drawerEditBoolen = !this.drawerEditBoolen;
        this.current = null;
      }
    },
    refuseOrder (row) {
      this.refuseOrderId = row.order_id
      this.centerDialogVisible = !this.centerDialogVisible
    },

    // /* 更改状态 */
    // editStatus (row) {
    //   const loading = this.$loading({ lock: true });
    //   upClassify({
    //     id: row.id,
    //     status: row.status,

    //   })
    //     .then((msg) => {
    //       console.log(msg)
    //       loading.close();
    //       this.$message.success(msg.message);
    //       this.reload();
    //     })
    //     .catch((e) => {
    //       loading.close();
    //       row.status = !row.status ? 1 : 2;
    //       this.$message.error(e.message);
    //     });
    // },

    /* 显示分配权限 */
    openAuth (row) {
      this.current = row;
      this.showAuth = true;
    },
    /* 删除 */
    // remove (row) {
    //   const loading = this.$loading({ lock: true });
    //   upClassify({
    //     id: row.id,
    //     status: 0,

    //   })
    //     .then((msg) => {
    //       loading.close();
    //       this.$message.success(msg);
    //       this.reload();
    //     })
    //     .catch((e) => {
    //       loading.close();
    //       this.$message.error(e.message);
    //     });
    // },
    /* 批量删除 */
    // removeBatch () {
    //   if (!this.selection.length) {
    //     this.$message.error('请至少选择一条数据');
    //     return;
    //   }
    //   console.log(this.selection);
    //   this.$confirm('确定要删除选中的信息吗?', '提示', {
    //     type: 'warning'
    //   })
    //     .then(() => {
    //       const loading = this.$loading({ lock: true });
    //       console.log(this.selection.map((d) => d.id));
    //       siteBatchCategoriesDelete(this.selection.map((d) => d.id))
    //         .then((msg) => {
    //           loading.close();
    //           this.$message.success(msg);
    //           this.reload();
    //         })
    //         .catch((e) => {
    //           loading.close();
    //           this.$message.error(e.message);
    //         });
    //     })
    //     .catch(() => { });
    // }
  }
};
	</script>
	
<style lang="less">
.categoryImages {
  width: 50px;
  height: 50px;
}
.realNameStyle {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  .avaterName {
    width: 32px;
    height: 32px;
    border-radius: 32px;
    vertical-align: middle;
    margin-right: 5px;
  }
}
</style>
	